const checkbox = document.querySelector("#myCheckbox");
const station1 = document.querySelector('#station1');
const identity1 = document.querySelector('#identity1');
const station1Error = document.querySelector('#station1-errorMessage')
const identity1Error = document.querySelector('#identity1-errorMessage')
const passwordPrompt = document.querySelector('#password-promptMessage')
const passwordError = document.querySelector('#password-errorMessage')
const inputElements = document.querySelectorAll('.input');
const select_options = document.querySelector('.select-options')
const numberRegex1 = /^\d{3}$/;
const numberRegex2 = /^\d{6}$/;
const letterRegex = /^[\u4E00-\u9FA5]{5}$/;

(function () {
  const login = document.querySelector('#login');
  const password = document.querySelector('#password');
  window.addEventListener('load', () => {
    document.querySelector('#station1').focus();
    if (localStorage.getItem("rememberMe") === "true") {
      checkbox.checked = true;
      station1.value = localStorage.getItem("savedStation1");
      identity1.value = localStorage.getItem("savedIdentity1");
      password.value = localStorage.getItem("savedPassword");
      document.querySelector('#station1').blur();
    }
    inputElements.forEach(element => {
      if (element.value !== '') {
        element.style.backgroundColor = 'rgba(22, 31, 32)';
        element.style.boxShadow = '0 4px 9px #161818';
      }
      else {
        element.style.backgroundColor = 'rgba(35,39,46,0.5)';
      }
    });
  });

  login.addEventListener('click', () => {
    const station1Value = station1.value;
    const identity1Value = identity1.value;
    const passwordValue = password.value;
    let flag = true;
    station1Error.classList.remove('block');
    identity1Error.classList.remove('block');
    passwordError.classList.remove('block');
    passwordPrompt.classList.remove('block');
    if (station1Value.trim() === '') {
      station1Error.classList.add('block');
      station1Error.innerHTML = '工号信息不能为空';
      flag = false;
    }
    else if (!numberRegex1.test(station1Value)) {
      station1Error.classList.add('block');
      station1Error.innerHTML = '工号信息格式错误';
      flag = false;
    }
    if (identity1Value.trim() === '') {
      identity1Error.classList.add('block');
      identity1Error.innerHTML = '身份信息不能为空';
      flag = false;
    }
    else if (!letterRegex.test(identity1Value)) {
      identity1Error.classList.add('block');
      identity1Error.innerHTML = '身份信息格式错误';
      flag = false;
    }
    if (passwordValue.trim() === '') {
      passwordPrompt.classList.remove('block');
      passwordError.classList.add('block');
      passwordError.innerHTML = '密码信息不能为空';
      flag = false;
    }
    else if (!numberRegex2.test(passwordValue)) {
      passwordPrompt.classList.remove('block');
      passwordError.classList.add('block');
      passwordError.innerHTML = '密码信息格式错误';
      flag = false;
    }
    if (!flag) {
      console.log('登录失败');
    }
    else {
      const userData = {
        station: station1Value,
        identity: identity1Value,
        password: passwordValue
      };
      const url = "/admin/login";
      axios.post(url, userData).then(function (res) {
        if (res.data.code == 1) {
          if (checkbox.checked) {
            localStorage.setItem("rememberMe", "true");
            localStorage.setItem("savedStation1", station1Value);
            localStorage.setItem("savedIdentity1", identity1Value);
            localStorage.setItem("savedPassword", passwordValue);
          } else {
            localStorage.setItem("rememberMe", "false");
            localStorage.setItem("savedStation1", "");
            localStorage.setItem("savedIdentity1", "");
            localStorage.setItem("savedPassword", "");
          }
          localStorage.setItem("savedToken", res.data.data.token);
          alert('身份认证成功，欢迎登录后台管理系统')
          // window.location.href = "../../Main/Home/Home.html";
        }
        else {
          if (res.data.data > 4) {
            alert(res.data.msg);
          }
          else {
            if (res.data.data == null) {
              alert(res.data.msg);
            }
            else {
              const time = 4 - res.data.data;
              alert("信息填写有误，您还有" + time + "次尝试机会",);
            }
          }
        }
      });
    }
  });

  const eyes = document.querySelector('#eyes');
  eyes.addEventListener('click', () => {
    if (password.type == 'password') {
      password.type = 'text';
      eyes.classList.remove('icon-icon_eyes_close');
      eyes.classList.add('icon-icon_eyes_open');
    }
    else {
      eyes.classList.remove('icon-icon_eyes_open');
      eyes.classList.add('icon-icon_eyes_close');
      password.type = 'password';
    }
  });
  password.addEventListener('click', () => {
    if (!numberRegex2.test(password.value)) {
      passwordError.classList.remove('block');
      passwordPrompt.classList.add('block')
      passwordPrompt.innerHTML = '请输入六位数纯数字密码';
    }
  });
  password.addEventListener('input', () => {
    passwordPrompt
    if (numberRegex2.test(password.value)) {
      passwordPrompt.classList.remove('block')
    }
    else {
      passwordPrompt.classList.add('block')
    }
  });

})();


(function () {
  const clear = document.querySelector('#clear');
  inputElements.forEach(element => {
    element.addEventListener('input', () => {
      if (element.value !== '') {
        element.style.backgroundColor = 'rgba(22, 31, 32)';
        element.style.boxShadow = '0 4px 9px #161818';
      }
      else {
        element.style.backgroundColor = 'rgba(35,39,46,0.5)';
      }
    });
  });
  clear.addEventListener('click', () => {
    inputElements.forEach(element => {
      element.value = '';
      station1Error.classList.remove('block')
      identity1Error.classList.remove('block')
      passwordPrompt.classList.remove('block')
      passwordError.classList.remove('block')
      station2Error.classList.remove('block')
      identity2Error.classList.remove('block')
      checkbox.checked = false
      element.style.backgroundColor = 'rgba(35,39,46,0.5)';
    });
  });
})();

function selectOption(option) {
  identity1.value = option;
  identity1.style.backgroundColor = 'rgba(22, 31, 32)';
  identity1.style.boxShadow = '0 4px 9px #161818';
}

station1.addEventListener('click', () => {
  station1Error.classList.remove('block')
});
identity1.addEventListener('click', () => {
  identity1Error.classList.remove('block')
  select_options.classList.add('block')
});
identity1.addEventListener('blur', () => {
  identity1Error.classList.remove('block')
  select_options.classList.remove('block')
});





